#!/bin/bash
echo "=============="
echo "1.增加用户并设置密码"
echo "2.删除用户"
echo "3.查看用户"
echo "4.退出"
echo "=============="

add_user(){
	read -p "请输入想要添加的用户名：" username
	if id $username &> /etc/null
	then	
		echo "用户$username已存在"
	else
		useradd $username
		echo "用户添加成功"
		read -s  -p "请输入密码：" user_pwd
		echo $user_pwd|passwd $username --stdin &> /etc/null
		echo "设置密码成功"
	fi
}
del_user(){
	read -p "请输入想要删除的用户名：" username
	if id $username &> /etc/null
	then	
		userdel -r $username
		echo "删除成功"
	else
		echo "用户$username不存在，无法删除"
	fi
}
check_user(){
	read -p "请输入想要查看的用户名：" username
	if id $username &> /etc/null
	then	
		id $username
	else
		echo "用户$username不存在，无法查看"
	fi
}

while true
do
	read -p "请输入想要实现的功能:" choice
	case $choice in
	1)
		add_user
		;;
	2)
		del_user
		;;
	3)
		check_user
		;;
	4)
		echo "退出"
		break
		;;

	esac
done
